package com.eshipenet.shipnet.equipmentextbuilder

import scala.util.Try

sealed trait EquipmentAction {
  val id: String
  val raw: DebeziumParser.Action
  val actionInfo: DebeziumActionInfo = DebeziumActionInfo(raw)
}

case class EquipmentInsert(id: String, after: Equipment, raw: DebeziumParser.Insert) extends EquipmentAction
case class EquipmentUpdate(id: String, before: Equipment, after: Equipment, raw: DebeziumParser.Update)
    extends EquipmentAction
case class EquipmentDelete(id: String, before: Equipment, raw: DebeziumParser.Delete) extends EquipmentAction

object EquipmentAction {

  def apply(act: DebeziumParser.Action): Try[EquipmentAction] = act match {
    case insert @ DebeziumParser.Insert(after, _) =>
      Equipment(after).map(e => EquipmentInsert(e.id, e, insert))
    case update @ DebeziumParser.Update(before, after, _) =>
      Try {
        val boxBefore = Equipment(before).get
        val boxAfter  = Equipment(after).get
        EquipmentUpdate(boxAfter.id, boxBefore, boxAfter, update)
      }
    case delete @ DebeziumParser.Delete(before, _) =>
      Equipment(before).map(e => EquipmentDelete(e.id, e, delete))
  }

}
